package com.platform.service.impl;

import com.platform.dao.WithdrawDao;
import com.platform.entity.UserEntity;
import com.platform.entity.UserTransactionRecordEntity;
import com.platform.entity.WithdrawEntity;
import com.platform.entity.WithdrawEntity;
import com.platform.service.UserService;
import com.platform.service.UserTransactionRecordService;
import com.platform.service.WithdrawService;
import com.platform.utils.R;
import com.platform.utils.RRException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Service实现类
 *
 * @author lipengjun
 * @email 939961241@qq.com
 * @date 2017-08-16 15:02:28
 */
@Service("withdrawService")
public class WithdrawServiceImpl implements WithdrawService {
    @Autowired
    private WithdrawDao withdrawDao;

    @Autowired
    private UserService userService;

    @Autowired
    private UserTransactionRecordService userTransactionRecordService;

    @Override
    public WithdrawEntity queryObject(Integer id) {
        return withdrawDao.queryObject(id);
    }

    @Override
    public List<WithdrawEntity> queryList(Map<String, Object> map) {
        return withdrawDao.queryList(map);
    }

    @Override
    public int queryTotal(Map<String, Object> map) {
        return withdrawDao.queryTotal(map);
    }

    @Override
    public int save(WithdrawEntity withdrawEntity) {
        withdrawEntity.setApplyDate(new Date());
        return withdrawDao.save(withdrawEntity);
    }

    @Override
    public int update(WithdrawEntity user) {
        return withdrawDao.update(user);
    }

    @Override
    public int delete(Integer id) {
        return withdrawDao.delete(id);
    }

    @Override
    public int deleteBatch(Integer[] ids) {
        return withdrawDao.deleteBatch(ids);
    }

    @Override
    @Transactional
    public R deal(Long id) {
        WithdrawEntity dbValue = queryObject(id.intValue());
        if (dbValue.getStatus().equals(WithdrawEntity.STATUS_DEAL)){
            return R.error("当前提现已打款，无需处理");
        }

        UserEntity userEntity = userService.queryObject(dbValue.getApplyUserId().intValue());
        if (userEntity.getAccount().compareTo(dbValue.getApplyMoney()) < 0){
            return R.error("用户当前余额不足，无法打款");
        }


        dbValue.setStatus(WithdrawEntity.STATUS_DEAL);
        update(dbValue);

        int result = userService.decreaseAccount(dbValue.getApplyUserId(), dbValue.getApplyMoney());
        if (result == 0){
            throw new RRException("用户当前余额不足，无法打款");
        }

        //保存折扣交易记录
        UserTransactionRecordEntity userTransactionRecordEntity = new UserTransactionRecordEntity();
        userTransactionRecordEntity.setMoney(BigDecimal.ZERO.subtract(dbValue.getApplyMoney()));
        userTransactionRecordEntity.setUserId(dbValue.getApplyUserId());
        userTransactionRecordEntity.setTransactionUserId(dbValue.getApplyUserId());
        userTransactionRecordEntity.setType(UserTransactionRecordEntity.TRANSACTION_TYPE_WITHDRAW );
        userTransactionRecordEntity.setRemark("提现" + dbValue.getApplyMoney().setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString() + "元");
        userTransactionRecordEntity.setOrderGoodsId(-1l);
        userTransactionRecordEntity.setOrderId(-1l);
        userTransactionRecordService.save(userTransactionRecordEntity);

        return R.ok();
    }
}
